package control;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import model.Element;
import model.ElementFactory;
import model.ElementManager;
import utilities.Calculator;

/**
 * Calls the model to read in stock data from the file and put it
 * into a readable form, and then pass it through the controller
 * to the view.
 * 
 * @author Group 8
 * 
 */
public class StockController extends Controller {
    private final String myGOOGLE = "http://www.google.com/finance/historical?output=csv&q=";
    private final String myYAHOO = "http://ichart.finance.yahoo.com/table.csv?" +
            "d=6&e=1&f=2009&g=d&a=7&b=19&c=2004&ignore=.csv&s=";

	public StockController () {
		super();
	}
	
	/**
	 * Overrides the promptForURL method in the Controller superclass. 
	 */
	protected String promptForURL () {
        String url = myView.promptURL();
        if (url.contains("google") || "1".equals(url)) {
            url = myGOOGLE;
        }
        else if (url.contains("yahoo") || "2".equals(url)) {
            url = myYAHOO;
        }
        return url;
    }

    /**
     * Returns a list of stock prices for a data set.
     * 
     * @param name name of the data set
     */
    public List<Integer> getElementData (String name) {
        return getManager().getElementData(name);
    }

    /**
     * Reads input and creates List of Elements.
     * 
     * @param em
     * @param fileName
     * @param url
     * @param ext
     * @return
     */
    protected List<Element> processInput (ElementManager em, String fileName,
            String url, String ext) {
        // calls appropriate methods in Element Factory
        // opens and reads in file and creates List of Elements
        // returns unmodifiable List of Elements
        try {
            Scanner scan = new Scanner(new File(fileName));
            while (scan.hasNextLine()) {
                String ticker = scan.next();
                String fullName = scan.nextLine();
                ElementFactory.loadNewElement(em, fullName, ticker, url +
                        ticker, ext);
            }
            scan.close();
        }
        catch (IOException e) {
            System.out.println("Difficulty Reading Stock Codes");
        }
        em.specifyData("open");
        em.sort(false);
        return em.getUnmodifiableList();
    }

    /**
     * 
     * public void Main(String[] args){
     * myView = new View("average price");
     * String filename = myView.promptName(); //FILENAME WITH ALL TICKER FILES
     * YOU WOULD LIKE TO LOAD
     * String url = myView.promptURL();
     * String ext = myView.promptExt();
     * ElementManager em = new ElementManager();
     * List<Element> myElements = initiate(em, filename, url, ext);
     * myView.createVisualization(myElements);
     * }
     */

}
